package io.renren.modules.jxgkphone.controller;
import io.renren.modules.jxgk.service.WebSocket;
import io.renren.modules.jxgkphone.dao.CourseDateMapper;
import io.renren.modules.jxgkphone.entity.CourseDate;
import io.renren.modules.jxgkphone.service.CourseDateService;
import io.renren.modules.jxgkphone.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/courseDate")
public class CourseDateController {
    @Autowired
    CourseDateService courseDateService;
    @Autowired
    CourseDateMapper courseDateMapper;
    @Autowired
    WebSocket webSocket;
    @RequestMapping(value = "/getDayInfo/{curDate}")
    @ResponseBody
    public Object getDayInfo(  @PathVariable("curDate") String curDate){
        return courseDateService.getDayInfo(curDate);
    }
    public String getBeginDate(String curDate) throws ParseException {
        String year="",month="",day="";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1=Calendar.getInstance();
        calendar1.setTime(sdf.parse(curDate));
        if(calendar1.get(Calendar.MONTH)>=8){
            //year=calendar1.get(Calendar.YEAR)
        }
        return curDate;
    }
    @RequestMapping(value = "/getDaysByTermAndWeek")
    @ResponseBody
    public Object getDaysByTermAndWeek(@RequestBody Map<String,Object> reqMap){
        Map<String,Object> map=new HashMap<String,Object>();
        String year=(String)reqMap.get("year");
        String term=(String)reqMap.get("term");
        String week=(String)reqMap.get("week");
        List<CourseDate> list=courseDateService.getDaysByTermAndWeek(year,term,week);
        map.put("date",list);
        return map;
    }
    @RequestMapping("/insertCourseDate")
    public String insert(@RequestBody Map<String,Object> reqMap ){
        String beginDate=(String)reqMap.get("beginDate");
        String endDate=(String)reqMap.get("endDate");
        try {
            Calendar start = Calendar.getInstance();
            String begins[] = beginDate.split("-");
            String ends[] = endDate.split("-");
            if (begins.length != 3 || ends.length != 3) {
                System.out.println("输入格式错误，请重新输入！");
                return "输入格式错误，请重新输入！";
            }
            start.set(Integer.parseInt(begins[0]), Integer.parseInt(begins[1]) - 1, Integer.parseInt(begins[2]));
            Long startTIme = start.getTimeInMillis();

            Calendar end = Calendar.getInstance();
            end.set(Integer.parseInt(ends[0]), Integer.parseInt(ends[1]) - 1, Integer.parseInt(ends[2]));
            Long endTime = end.getTimeInMillis();

            Long oneDay = 1000 * 60 * 60 * 24L;

            Long time = startTIme;
            //总天数
            double days=(endTime-startTIme)/oneDay;
            //当前天数
            double currentDay=0;
            while (time <= endTime) {
                Date d = new Date(time);
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                Map<String, String> map = Utils.getDayInfo(df.format(d), beginDate);
                //如果跨年
                String formatYear=map.get("year").substring(0,4);
                String startYear=beginDate.substring(0,4);
                //如果是第一学期 且日期来到了第二年
                if(!formatYear.equals(startYear)&&reqMap.get("term").equals("1")){
                    int year=Integer.parseInt(startYear);
                    int afterYear=year+1;
                    String fullYear=year+"-"+afterYear;
                    map.put("year",fullYear);
                }
                System.out.println("date:"+map.get("date")+"year:"+map.get("year")+"term:"+map.get("term")+"week:"+map.get("week")+"day:"+map.get("day"));

                courseDateService.insertCourseDate(map.get("date"), map.get("year"), map.get("term"), map.get("week"), map.get("day"));

                currentDay++;
                double percentage=currentDay/days*100;
                //保留两位小数
                BigDecimal b = new BigDecimal(percentage);
                double r = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                webSocket.sendMessage(""+r);
                time += oneDay;
            }
        }catch (Exception e){
            return "error";
        }
        return "success";
    }
}
